﻿// BizElements generated entity and metadata classes for VAdmUserInfo.
using System;
using System.Data;
using System.Configuration;
using BizElements.Core;

namespace BizElements.BusinessLayer
{
	/// <summary>
	/// Entity model class for the table/view 'VAdmUserInfo'.
	/// </summary>
	[Serializable]
	internal class VAdmUserInfoEntity : EntityModelBase
	{
		#region Private members.

		// Members mapped to database columns.
		private int? userId;
		private string username;
		private string fullName;
		private int? organizationId;
		private string organizationName;
		private int? businessUnitId;
		private string businessUnitName;
		private string languageCode;
		private string cultureCode;
		
		
		#endregion

		#region Protected properties.
    
		// Properties mapped to database columns.    
		/// <summary>
		/// Gets or sets the value which is mapped to the field 'UserId'.
		/// </summary>
		protected int? _UserId
		{
			get
			{
				return this.userId;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if ((!value.HasValue != !this.userId.HasValue) || (value.HasValue && (value.Value != this.userId.Value)))
						this.EntityState = EntityState.OutOfSync;
				}
				this.userId = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'Username'.
		/// </summary>
		protected string _Username
		{
			get
			{
				return this.username;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.username)
						this.EntityState = EntityState.OutOfSync;
				}
				this.username = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'FullName'.
		/// </summary>
		protected string _FullName
		{
			get
			{
				return this.fullName;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.fullName)
						this.EntityState = EntityState.OutOfSync;
				}
				this.fullName = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'OrganizationId'.
		/// </summary>
		protected int? _OrganizationId
		{
			get
			{
				return this.organizationId;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if ((!value.HasValue != !this.organizationId.HasValue) || (value.HasValue && (value.Value != this.organizationId.Value)))
						this.EntityState = EntityState.OutOfSync;
				}
				this.organizationId = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'OrganizationName'.
		/// </summary>
		protected string _OrganizationName
		{
			get
			{
				return this.organizationName;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.organizationName)
						this.EntityState = EntityState.OutOfSync;
				}
				this.organizationName = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'BusinessUnitId'.
		/// </summary>
		protected int? _BusinessUnitId
		{
			get
			{
				return this.businessUnitId;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if ((!value.HasValue != !this.businessUnitId.HasValue) || (value.HasValue && (value.Value != this.businessUnitId.Value)))
						this.EntityState = EntityState.OutOfSync;
				}
				this.businessUnitId = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'BusinessUnitName'.
		/// </summary>
		protected string _BusinessUnitName
		{
			get
			{
				return this.businessUnitName;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.businessUnitName)
						this.EntityState = EntityState.OutOfSync;
				}
				this.businessUnitName = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'LanguageCode'.
		/// </summary>
		protected string _LanguageCode
		{
			get
			{
				return this.languageCode;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.languageCode)
						this.EntityState = EntityState.OutOfSync;
				}
				this.languageCode = value;
			}
		}

		/// <summary>
		/// Gets or sets the value which is mapped to the field 'CultureCode'.
		/// </summary>
		protected string _CultureCode
		{
			get
			{
				return this.cultureCode;
			}
			set
			{
				if (this.EntityState == EntityState.Synchronized)
				{
					if (value != this.cultureCode)
						this.EntityState = EntityState.OutOfSync;
				}
				this.cultureCode = value;
			}
		}

		
		#endregion

		#region Constructors.

		/// <summary>
		/// Default constructor.
		/// </summary>
		public VAdmUserInfoEntity() 
			 : base(new VAdmUserInfoMeta())
		{
		}

		/// <summary>
		/// Constructor. Initalizes Table property with the specified (shared) instance.
		/// </summary>
		/// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
		public VAdmUserInfoEntity(VAdmUserInfoMeta dbTable)
			: base(dbTable)
		{
		}
		
		/// <summary>
		/// Constructor. Initializes members with the values in the given DataRow.
		/// </summary>
		/// <param name="row">DataRow with required columns.</param>
		/// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
		public VAdmUserInfoEntity(DataRow row) 
			 : base(new VAdmUserInfoMeta())
		{
			FromDataRow(row);
		}

		/// <summary>
		/// Constructor. Initializes members with the values in the given DataRow.
		/// </summary>
		/// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
		/// <param name="row">DataRow with required columns.</param>
		/// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
		public VAdmUserInfoEntity(VAdmUserInfoMeta dbTable, DataRow row) 
			 : base(dbTable)
		{
			FromDataRow(row);
		}

		/// <summary>
		/// Copy constructor.
		/// </summary>
		/// <param name="existing">Instance of VAdmUserInfoEntity.</param>
		public VAdmUserInfoEntity(VAdmUserInfoEntity existing) 
			 : base(existing.Table)
		{
			 FromExistingEntity(existing);
		}
    
		static readonly Type DbTableClass = typeof(VAdmUserInfoMeta);

		/// <summary>
		/// Initializes all members of VAdmUserInfoEntity and base EntityModelBase class. This is the fastest way to initialize entity with data from database.
		/// </summary>
		private VAdmUserInfoEntity(IDbTable table, EntityState entityState, int? userId, string username, string fullName, int? organizationId, string organizationName, int? businessUnitId, string businessUnitName, string languageCode, string cultureCode) 
			 : base(table, DbTableClass, entityState)
		{
			this.userId = userId;
			this.username = username;
			this.fullName = fullName;
			this.organizationId = organizationId;
			this.organizationName = organizationName;
			this.businessUnitId = businessUnitId;
			this.businessUnitName = businessUnitName;
			this.languageCode = languageCode;
			this.cultureCode = cultureCode;
		}

		/// <summary>
		/// Initializes all members mapped to fields.
		/// </summary>
		protected void Init(int? userId, string username, string fullName, int? organizationId, string organizationName, int? businessUnitId, string businessUnitName, string languageCode, string cultureCode)
		{
			_UserId = userId;
			_Username = username;
			_FullName = fullName;
			_OrganizationId = organizationId;
			_OrganizationName = organizationName;
			_BusinessUnitId = businessUnitId;
			_BusinessUnitName = businessUnitName;
			_LanguageCode = languageCode;
			_CultureCode = cultureCode;
		}

		#endregion

		#region Methods.

		/// <summary>
		/// Gets the value(s) that uniquely identifiy an entity.
		/// In the order as specified in accompanying IDbTable metadata class.
		/// NULL if the parent table/view doesn't have a primary key constraint or the required fields are not set.
		/// </summary>
		public override object[] GetPrimaryKeyValue()
		{
			return null;
		}

		/// <summary>
		/// Initializes entity members with data stored in the given DataRow.
		/// </summary>
		/// <param name="row">DataRow with the required columns.</param>
		/// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
		public override void FromDataRow(DataRow row)
		{
			object currentColumnValue;
      
			currentColumnValue = TryGetColumnValue(row, "UserId");
			_UserId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "Username");
			_Username = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "FullName");
			_FullName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "OrganizationId");
			_OrganizationId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "OrganizationName");
			_OrganizationName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "BusinessUnitId");
			_BusinessUnitId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "BusinessUnitName");
			_BusinessUnitName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "LanguageCode");
			_LanguageCode = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumnValue = TryGetColumnValue(row, "CultureCode");
			_CultureCode = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
			if (setEntityStateToSynchronized)
				this.EntityState = EntityState.Synchronized;
		}

		private static object TryGetColumnValue(DataRow row, string columnName)
		{
			int colIdx = row.Table.Columns.IndexOf(columnName);
			return (colIdx >= 0) ? row[colIdx] : null;
		}

		private static object GetColumnValue(DataRow row, IDbColumn column)
		{
			int colIdx = row.Table.Columns.IndexOf(column.Alias);
			if (colIdx < 0)
				colIdx = row.Table.Columns.IndexOf(column.ColumnName);
			if (colIdx < 0)
				throw new ArgumentException("DataTable doesn't contain the specified column (" + column.ColumnName + ").");

			return row[colIdx];
		}

		/// <summary>
		/// Initializes entity members with data stored in the given DataRow.
		/// </summary>
		/// <param name="row">DataRow with all or some of the columns defined in meta data.</param>
		/// <param name="fieldMetaData"><see cref="IDbTable"/> meta data object which links ADO.NET row columns to entity properties.</param>
		/// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
		public override void FromDataRow(DataRow row, IDbTable fieldMetaData)
		{
			IDbColumn currentColumn;
			object currentColumnValue;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.UserId.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_UserId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Username.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_Username = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.FullName.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_FullName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.OrganizationId.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_OrganizationId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.OrganizationName.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_OrganizationName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.BusinessUnitId.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_BusinessUnitId = (currentColumnValue != DBNull.Value) ? (int?) Convert.ToInt32(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.BusinessUnitName.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_BusinessUnitName = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.LanguageCode.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_LanguageCode = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.CultureCode.PropertyName);
			currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
			_CultureCode = (currentColumnValue != DBNull.Value) ? (string) Convert.ToString(currentColumnValue) : null;
      
			bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
			if (setEntityStateToSynchronized)
				this.EntityState = EntityState.Synchronized;
		}

		/// <summary>
		/// Converts the given DataRow to VAdmUserInfoEntity.
		/// </summary>
		/// <param name="row">DataRow with required columns.</param>
		/// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
		public static explicit operator VAdmUserInfoEntity(DataRow row)
		{
			return new VAdmUserInfoEntity(row);
		}

		/// <summary>
		/// Creates an array of objects containing entity data.
		/// </summary>
		/// <returns>Entity values.</returns>
		public override object[] ToObjectArray()
		{
			object[] values = new object[9];
      
			values[0] = (_UserId != null) ? (object) _UserId.Value : null;
			values[1] = (_Username != null) ? (object) _Username : null;
			values[2] = (_FullName != null) ? (object) _FullName : null;
			values[3] = (_OrganizationId != null) ? (object) _OrganizationId.Value : null;
			values[4] = (_OrganizationName != null) ? (object) _OrganizationName : null;
			values[5] = (_BusinessUnitId != null) ? (object) _BusinessUnitId.Value : null;
			values[6] = (_BusinessUnitName != null) ? (object) _BusinessUnitName : null;
			values[7] = (_LanguageCode != null) ? (object) _LanguageCode : null;
			values[8] = (_CultureCode != null) ? (object) _CultureCode : null;
			
			return values;
		}

		/// <summary>
		/// Initializes entity members with the given values.
		/// </summary>
		/// <param name="entityValues">Array with the required values.</param>
		public override void FromObjectArray(object[] entityValues)
		{
			_UserId = (entityValues[0] != null) ? (int?) Convert.ToInt32(entityValues[0]) : null;
			_Username = (entityValues[1] != null) ? (string) Convert.ToString(entityValues[1]) : null;
			_FullName = (entityValues[2] != null) ? (string) Convert.ToString(entityValues[2]) : null;
			_OrganizationId = (entityValues[3] != null) ? (int?) Convert.ToInt32(entityValues[3]) : null;
			_OrganizationName = (entityValues[4] != null) ? (string) Convert.ToString(entityValues[4]) : null;
			_BusinessUnitId = (entityValues[5] != null) ? (int?) Convert.ToInt32(entityValues[5]) : null;
			_BusinessUnitName = (entityValues[6] != null) ? (string) Convert.ToString(entityValues[6]) : null;
			_LanguageCode = (entityValues[7] != null) ? (string) Convert.ToString(entityValues[7]) : null;
			_CultureCode = (entityValues[8] != null) ? (string) Convert.ToString(entityValues[8]) : null;
		}

		/// <summary>
		/// Gets the parent entity defined by the given foreign key.
		/// </summary>
		/// <param name="foreignKey">FK which must be defined in the VAdmUserInfoMeta class or an exception is generated.</param>
		/// <returns>Parent entity. NULL if the FK fields haven't been set or if the entity with the given key values doesn't exist.</returns>
		public override IEntity GetParent(DbRelation foreignKey)
		{
			throw new Exception("No foreign keys are defined in the VAdmUserInfoMeta class.");
		}

		/// <summary>
		/// Sets the given value into the member that represents the parent entity defined by the foreign key.
		/// </summary>
		/// <param name="foreignKey">FK which must be defined in the CFunctionsMeta class or an exception is generated.</param>
		/// <param name="entity">Parent entity. May be NULL. Must be an instance of the CFunctionsEntity or a derived class.</param>
		public override void SetParent(DbRelation foreignKey, IEntity entity)
		{
			throw new Exception("No foreign keys are defined in the VAdmUserInfoMeta class.");
		}

		#endregion

		/// <summary>
		/// Gets typed IDbTable object for the entity's table/view.
		/// </summary>
		public VAdmUserInfoMeta Table
		{
			get {return (VAdmUserInfoMeta) _Table;}
		}

		#region Public properties mapped to database columns.
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'UserId'.
		/// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
		/// </summary>
		public virtual int UserId
		{
			get
			{
				if (_UserId == null)
				{
					if (this.NullCheckEnabled)
						throw new NoNullAllowedException("VAdmUserInfoEntity.get_UserId: UserId is not set yet.");
					else
						return default(int);
				}
        
				return _UserId.Value;
			}
			set
			{
				_UserId = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'Username'.
		/// </summary>
		public virtual string Username
		{
			get
			{
				return _Username;
			}
			set
			{
				_Username = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'FullName'.
		/// </summary>
		public virtual string FullName
		{
			get
			{
				return _FullName;
			}
			set
			{
				_FullName = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'OrganizationId'.
		/// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
		/// </summary>
		public virtual int OrganizationId
		{
			get
			{
				if (_OrganizationId == null)
				{
					if (this.NullCheckEnabled)
						throw new NoNullAllowedException("VAdmUserInfoEntity.get_OrganizationId: OrganizationId is not set yet.");
					else
						return default(int);
				}
        
				return _OrganizationId.Value;
			}
			set
			{
				_OrganizationId = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'OrganizationName'.
		/// </summary>
		public virtual string OrganizationName
		{
			get
			{
				return _OrganizationName;
			}
			set
			{
				_OrganizationName = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'BusinessUnitId'.
		/// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
		/// </summary>
		public virtual int BusinessUnitId
		{
			get
			{
				if (_BusinessUnitId == null)
				{
					if (this.NullCheckEnabled)
						throw new NoNullAllowedException("VAdmUserInfoEntity.get_BusinessUnitId: BusinessUnitId is not set yet.");
					else
						return default(int);
				}
        
				return _BusinessUnitId.Value;
			}
			set
			{
				_BusinessUnitId = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'BusinessUnitName'.
		/// </summary>
		public virtual string BusinessUnitName
		{
			get
			{
				return _BusinessUnitName;
			}
			set
			{
				_BusinessUnitName = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'LanguageCode'.
		/// </summary>
		public virtual string LanguageCode
		{
			get
			{
				return _LanguageCode;
			}
			set
			{
				_LanguageCode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the value which is mapped to the non-nullable field 'CultureCode'.
		/// </summary>
		public virtual string CultureCode
		{
			get
			{
				return _CultureCode;
			}
			set
			{
				_CultureCode = value;
			}
		}
		
		#endregion
		
		#region NewEntity static methods suitable for usage in EntityBuilder<T>.

		/// <summary>Creates new entity and initializes all members of with the given values.</summary>
		/// <param name="table">Metadata for table/view to which the entity belongs to.</param>
		/// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
		/// <param name="values">Array which contains values for all properties mapped to database columns in the following order: UserId, Username, FullName, OrganizationId, OrganizationName, BusinessUnitId, BusinessUnitName, LanguageCode, CultureCode.</param>
		/// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
		/// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
		/// with IDataConverter objects which retrieve property values directly from IDataReader objects.</remarks>
		public static VAdmUserInfoEntity NewEntity(IDbTable table, EntityState entityState, object[] values)
		{
			return new VAdmUserInfoEntity(table, entityState, (int)values[0], (string)values[1], (string)values[2], (int)values[3], (string)values[4], (int)values[5], (string)values[6], (string)values[7], (string)values[8]);
		}

		/// <summary>Creates new entity and initializes members for which values are defined in the array.</summary>
		/// <param name="table">Metadata for table/view to which the entity belongs to.</param>
		/// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
		/// <param name="values">Array which contains values or nulls for all properties mapped to database columns in the following order: UserId, Username, FullName, OrganizationId, OrganizationName, BusinessUnitId, BusinessUnitName, LanguageCode, CultureCode.</param>
		/// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
		/// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
		/// with IObjectReader objects which retrieve property values directly from IDataReader objects.</remarks>
		public static VAdmUserInfoEntity NewPartialEntity(IDbTable table, EntityState entityState, object[] values)
		{
			return new VAdmUserInfoEntity(table, entityState, CastTo<int>(values[0]), (string)ReplaceDbNull(values[1]), (string)ReplaceDbNull(values[2]), CastTo<int>(values[3]), (string)ReplaceDbNull(values[4]), CastTo<int>(values[5]), (string)ReplaceDbNull(values[6]), (string)ReplaceDbNull(values[7]), (string)ReplaceDbNull(values[8]));
		}

		private static T? CastTo<T>(object val)
			where T : struct
		{
			if (IsNull(val))
				return null;
			else
				return (T)val;
		}

		private static bool IsNull(object val)
		{
			return (val == DBNull.Value || val == null);
		}
    
		private static object ReplaceDbNull(object dbValue)
		{
			return (dbValue != DBNull.Value) ? dbValue : null;
		}

		#endregion
	}

	/// <summary>
	/// Typed IDbTable class for the table/view 'VAdmUserInfo'.
	/// </summary>
	[Serializable]
	public sealed class VAdmUserInfoMeta : SealedDbTable
	{
		static VAdmUserInfoMeta()
		{
			ReadSequenceOverrideFromConfig();
			ReadColumnNamesFromConfig();
			ImmutableColumnProperties = CreateImmutableColumnProperties();
			ImmutableTableProperties = CreateImmutableTableProperties();
		}
    
		#region Configuration.
		
		static readonly string[] SequenceNameOverrides = new string[9];
		static readonly bool?[] AutoIncrementOverrides = new bool?[9];
    
		private static void ReadSequenceOverrideFromConfig()
		{
			SequenceNameOverrides[0] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.UserId.SequenceName"];
			AutoIncrementOverrides[0] = (SequenceNameOverrides[0] != null) ? (bool?)true : null;
			SequenceNameOverrides[1] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.Username.SequenceName"];
			AutoIncrementOverrides[1] = (SequenceNameOverrides[1] != null) ? (bool?)true : null;
			SequenceNameOverrides[2] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.FullName.SequenceName"];
			AutoIncrementOverrides[2] = (SequenceNameOverrides[2] != null) ? (bool?)true : null;
			SequenceNameOverrides[3] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.OrganizationId.SequenceName"];
			AutoIncrementOverrides[3] = (SequenceNameOverrides[3] != null) ? (bool?)true : null;
			SequenceNameOverrides[4] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.OrganizationName.SequenceName"];
			AutoIncrementOverrides[4] = (SequenceNameOverrides[4] != null) ? (bool?)true : null;
			SequenceNameOverrides[5] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.BusinessUnitId.SequenceName"];
			AutoIncrementOverrides[5] = (SequenceNameOverrides[5] != null) ? (bool?)true : null;
			SequenceNameOverrides[6] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.BusinessUnitName.SequenceName"];
			AutoIncrementOverrides[6] = (SequenceNameOverrides[6] != null) ? (bool?)true : null;
			SequenceNameOverrides[7] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.LanguageCode.SequenceName"];
			AutoIncrementOverrides[7] = (SequenceNameOverrides[7] != null) ? (bool?)true : null;
			SequenceNameOverrides[8] = ConfigurationManager.AppSettings["BizElements.BusinessLayer.VAdmUserInfo.CultureCode.SequenceName"];
			AutoIncrementOverrides[8] = (SequenceNameOverrides[8] != null) ? (bool?)true : null;
		}

		static readonly string[] ColumnNames = new string[9];

		private static void ReadColumnNamesFromConfig()
		{
			ColumnNames[0] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "UserId") ?? "UserId";
			ColumnNames[1] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "Username") ?? "Username";
			ColumnNames[2] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "FullName") ?? "FullName";
			ColumnNames[3] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "OrganizationId") ?? "OrganizationId";
			ColumnNames[4] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "OrganizationName") ?? "OrganizationName";
			ColumnNames[5] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "BusinessUnitId") ?? "BusinessUnitId";
			ColumnNames[6] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "BusinessUnitName") ?? "BusinessUnitName";
			ColumnNames[7] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "LanguageCode") ?? "LanguageCode";
			ColumnNames[8] = BizElements.BusinessLayer.Catalog.GetColumnNameOverride("VAdmUserInfo", "CultureCode") ?? "CultureCode";
		}

		#endregion
		
		#region Singleton/immutable configuration objects.

		static readonly DbColumnConfiguration[] ImmutableColumnProperties;
		static readonly DbTableConfiguration ImmutableTableProperties;

		private static DbColumnConfiguration[] CreateImmutableColumnProperties()
		{
			return new DbColumnConfiguration[]
			{
				new DbColumnConfiguration(ColumnNames[0], DbType.Int32, typeof(int), false, 0, true, null, 0, false, false, false, "UserId", int.MinValue, int.MaxValue, false, SequenceNameOverrides[0]),
				new DbColumnConfiguration(ColumnNames[1], DbType.AnsiString, typeof(string), false, 1, AutoIncrementOverrides[1] ?? false, null, 128, false, false, false, "Username", null, null, false, SequenceNameOverrides[1]),
				new DbColumnConfiguration(ColumnNames[2], DbType.String, typeof(string), false, 2, AutoIncrementOverrides[2] ?? false, null, 256, false, false, false, "FullName", null, null, false, SequenceNameOverrides[2]),
				new DbColumnConfiguration(ColumnNames[3], DbType.Int32, typeof(int), false, 3, AutoIncrementOverrides[3] ?? false, null, 0, false, false, false, "OrganizationId", int.MinValue, int.MaxValue, false, SequenceNameOverrides[3]),
				new DbColumnConfiguration(ColumnNames[4], DbType.String, typeof(string), false, 4, AutoIncrementOverrides[4] ?? false, null, 256, false, false, false, "OrganizationName", null, null, false, SequenceNameOverrides[4]),
				new DbColumnConfiguration(ColumnNames[5], DbType.Int32, typeof(int), false, 5, AutoIncrementOverrides[5] ?? false, null, 0, false, false, false, "BusinessUnitId", int.MinValue, int.MaxValue, false, SequenceNameOverrides[5]),
				new DbColumnConfiguration(ColumnNames[6], DbType.String, typeof(string), false, 6, AutoIncrementOverrides[6] ?? false, null, 256, false, false, false, "BusinessUnitName", null, null, false, SequenceNameOverrides[6]),
				new DbColumnConfiguration(ColumnNames[7], DbType.AnsiStringFixedLength, typeof(string), false, 7, AutoIncrementOverrides[7] ?? false, null, 2, false, false, false, "LanguageCode", null, null, false, SequenceNameOverrides[7]),
				new DbColumnConfiguration(ColumnNames[8], DbType.AnsiString, typeof(string), false, 8, AutoIncrementOverrides[8] ?? false, null, 64, false, false, false, "CultureCode", null, null, false, SequenceNameOverrides[8]),
			};
		}

		private static DbTableConfiguration CreateImmutableTableProperties()
		{
			return new DbTableConfiguration
			(
				BizElements.BusinessLayer.Catalog.GetTableNameOverride("VAdmUserInfo") ?? "VAdmUserInfo",
				new BizElements.BusinessLayer.Catalog(),
				ImmutableColumnProperties,
				new int[] { },
				new string[] { },
                /*is view*/ true
			);
		}
		
		#endregion
		
		#region Constructors.

		/// <summary>
		/// Initializes a new instance of VAdmUserInfoMeta class.
		/// </summary>
		public VAdmUserInfoMeta()
			: base(ImmutableTableProperties, null, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of VAdmUserInfoMeta class.
		/// </summary>
		/// <param name="alias">Object alias. If NULL then it will be equal to the table name.</param>
		public VAdmUserInfoMeta(string alias)
			: base(ImmutableTableProperties, alias, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of VAdmUserInfoMeta class.
		/// </summary>
		/// <param name="setPrefixedColumnAliases">Whether to change aliases of all columns so that they start with prefix (usually table name).</param>
		public VAdmUserInfoMeta(bool setPrefixedColumnAliases)
			: base(ImmutableTableProperties, null, setPrefixedColumnAliases)
		{
		}

		/// <summary>
		/// Initializes a new instance of VAdmUserInfoMeta class.
		/// </summary>
		/// <param name="alias">Object alias. If NULL then it will be equal to the table name.</param>
		/// <param name="setPrefixedColumnAliases">Whether to change aliases of all columns so that they start with prefix (usually table name).</param>
		public VAdmUserInfoMeta(string alias, bool setPrefixedColumnAliases)
			: base(ImmutableTableProperties, alias, setPrefixedColumnAliases)
		{
		}

		#endregion

		#region CreateForeignKeys.

		/// <summary>
		/// Initializes an array of relations used by foreign keys.
		/// Only relations to other generated IDbTable classes are created.
		/// </summary>
		protected override DbRelation[] CreateForeignKeys()
		{
			DbRelation[] foreignKeys = new DbRelation[0];
      
			return foreignKeys;
		}

		#endregion

		#region New* methods.
    
		/// <summary>
		/// Creates a new VAdmUserInfoEntity object.
		/// </summary>
		/// <returns>New entity.</returns>
		public override IEntity NewEntity()
		{
			return new VAdmUserInfoEntity(this);
		}
    
		/// <summary>
		/// Creates a new EntityReader&lt;VAdmUserInfoEntity&gt; object.
		/// </summary>
		/// <returns>An instance of EntityReader&lt;VAdmUserInfoEntity&gt; class.</returns>
		public override IObjectReader NewEntityReader()
		{
			// Use default EntityReader<T>() constructor when targeting SQLite database or when column types are compatible/convertible, but do not exactly match, entity property types.
			return new EntityReader<VAdmUserInfoEntity>(VAdmUserInfoEntity.NewEntity);
		}

		#endregion

		#region Clone.

		/// <summary>
		/// Creates another IDbTable object for the same table/view.
		/// </summary>
		/// <param name="cloneAlias">Clone alias.</param>
		/// <returns>Clone.</returns>
		public override IDbTable Clone(string cloneAlias)
		{
			return new VAdmUserInfoMeta(cloneAlias);
		}

		/// <summary>
		/// Creates another IDbTable object for the same table/view.
		/// </summary>
		/// <param name="cloneAlias">Clone alias.</param>
		/// <param name="setPrefixedAliases">Specifies whether cloned columns will have prefixed aliases.</param>
		/// <returns>Clone.</returns>
		public override IDbTable Clone(string cloneAlias, bool setPrefixedAliases)
		{
			return new VAdmUserInfoMeta(cloneAlias, setPrefixedAliases);
		}

		#endregion

		#region Columns.
    
		/// <summary>
		/// Gets metadata for 'UserId' column.
		/// </summary>
		public IDbColumn UserId
		{
			get {return this.Columns[0];}
		}
			
		/// <summary>
		/// Gets metadata for 'Username' column.
		/// </summary>
		public IDbColumn Username
		{
			get {return this.Columns[1];}
		}
			
		/// <summary>
		/// Gets metadata for 'FullName' column.
		/// </summary>
		public IDbColumn FullName
		{
			get {return this.Columns[2];}
		}
			
		/// <summary>
		/// Gets metadata for 'OrganizationId' column.
		/// </summary>
		public IDbColumn OrganizationId
		{
			get {return this.Columns[3];}
		}
			
		/// <summary>
		/// Gets metadata for 'OrganizationName' column.
		/// </summary>
		public IDbColumn OrganizationName
		{
			get {return this.Columns[4];}
		}
			
		/// <summary>
		/// Gets metadata for 'BusinessUnitId' column.
		/// </summary>
		public IDbColumn BusinessUnitId
		{
			get {return this.Columns[5];}
		}
			
		/// <summary>
		/// Gets metadata for 'BusinessUnitName' column.
		/// </summary>
		public IDbColumn BusinessUnitName
		{
			get {return this.Columns[6];}
		}
			
		/// <summary>
		/// Gets metadata for 'LanguageCode' column.
		/// </summary>
		public IDbColumn LanguageCode
		{
			get {return this.Columns[7];}
		}
			
		/// <summary>
		/// Gets metadata for 'CultureCode' column.
		/// </summary>
		public IDbColumn CultureCode
		{
			get {return this.Columns[8];}
		}
			
		#endregion
    
		#region Foreign keys.
		
		#endregion

		#region Parent entity property name getters.
		
		#endregion

		#region Children.
    
		/// <summary>
		/// Gets tables which reference the current table.
		/// </summary>
		/// <returns>Array of tables or empty array if the current table is not referenced by other objects.</returns>
		public override IDbTable[] GetChildTables()
		{
			return new IDbTable[]
			{
			};
		}

		/// <summary>
		/// Gets relations where current table acts as a parent.
		/// </summary>
		/// <returns>Array of relations or empty array if the current table is not referenced by other objects.</returns>
		public override DbRelation[] GetChildRelations()
		{
			return new DbRelation[]
			{
			};
		}

		#endregion
	}
}
